package com.xzw.cameratest.push;

import android.content.Context;
import android.text.TextUtils;

import com.android.business.adapter.DataAdapterImpl;
import com.android.business.adapter.DataAdapterInterface;
import com.android.business.callback.IMessageCallback;
import com.android.business.common.PushMessageCode;
import com.android.business.exception.BusinessException;
import com.xzw.cameratest.utils.AndroidJsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created by 17801 on 2017/3/6.
 */
public class DSSPush implements PushInterface, IMessageCallback {

    private static final String TAG = DSSPush.class.getSimpleName();
    private Context mContext;
    /**把通知和报警消息队列分开，防止报警太多，通知消息处理不及时的问题；**/
    /**通知消息队列**/
    private LinkedBlockingDeque<String> msgList = new LinkedBlockingDeque<>();

    /**报警消息队列**/
    private LinkedBlockingDeque<String> alarmList = new LinkedBlockingDeque<>();

    private PushCallbackInterface pushCallbackInterface;

    private boolean bQuit = false;


    @Override
    public boolean init(Context context) {
        mContext = context;
        DataAdapterInterface dataAdapterInterface = DataAdapterImpl.getInstance();
        try {
            dataAdapterInterface.registerMessageCallback(this);
            dataAdapterInterface.registerAlarmMessageCallback(new AlarmCallBack());
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        initMsgLooper();
        return true;
    }

    private void initMsgLooper() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(!bQuit) {
                    String callMsg = null;
                    try {
                        callMsg  = msgList.take();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (!TextUtils.isEmpty(callMsg)) {
                        dealCallbackMsg(callMsg);
                    }
                }
            }
        }, "eventDispatchThread").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(!bQuit) {
                    String callMsg = null;
                    try {
                        callMsg = alarmList.take();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (!TextUtils.isEmpty(callMsg)) {
                        dealCallbackMsg(callMsg);
                    }
                }
            }
        }, "alarmDispatchThread").start();
    }

    
    @Override
    public String getUUID(Context mContext) throws BusinessException {
        return null;
    }

    @Override
    public String getRegistrationID(Context mContext) throws BusinessException {
        return null;
    }

    @Override
    public String getJpushTag(Context mContext) throws BusinessException {
        return null;
    }

    @Override
    public String getPushType() {
        return "DSSPush";
    }

    @Override
    public void setPushWatcher(PushCallbackInterface watcher) {
        this.pushCallbackInterface = watcher;
    }

    @Override
    public void callback(String jsonMsg) {
        try {
            msgList.put(jsonMsg);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addMsg(String jsonMsg) {
        try {
            msgList.put(jsonMsg);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void dealCallbackMsg(String jsonMsg) {
        int cmd ;
        try {
            JSONObject jsonObj = new JSONObject(jsonMsg);
            cmd = jsonObj.optInt("cmd");
            if (cmd == PushMessageCode.DPSDK_CMD_GENERALJSONTRANSPORT.getValue()) {
                String receive = jsonObj.optString("notify.data");
                handleGenJson(receive);
            } else if(cmd == PushMessageCode.DPSDK_CMD_SENDDATATOMDL.getValue()) {
                JSONObject jRootObj = AndroidJsonUtils.parseObject(jsonMsg);
                if(jRootObj != null) {
                    String method = jRootObj.optString("method");
                    pushCallbackInterface.callback(mContext, null, cmd, method, jsonMsg);
                }
            } else {
                pushCallbackInterface.callback(mContext, null, cmd, "", jsonMsg);
            }

        } catch (JSONException e) {
            e.printStackTrace();

        }
    }

    private void handleGenJson(String genJsonNotify) {
//        GeneralJsonNotify.getInstance().handleGeneralJsonNotify(mContext, genJsonNotify);
    }

     class AlarmCallBack implements IMessageCallback {

        @Override
        public void callback(String jsonMsg) {
            try {
                alarmList.put(jsonMsg);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
